﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Model;
using DataAccess;

namespace Project
{
    /// <summary>
    /// Lógica de interacción para SearchFunctionary.xaml
    /// </summary>
    public partial class SearchFunctionary : Window
    {
        public List<Functionary> allEmployees;
        public List<FunctionaryInfo> employeesToShow;
        public FunctionaryInfo Employee { get; set; }
        public event RoutedEventHandler getEmployee;

        public SearchFunctionary()
        {
            InitializeComponent();
            tbCI.IsEnabled = false;
            employeesToShow = new List<FunctionaryInfo>();
            tbCI.DeactivateBlankValidation();
            tbName.DeactivateBlankValidation();
            allEmployees = DataBaseAccess.GetAllActiveEmployees;
            fillInfoList();
            listViewEmployees.DataContext = employeesToShow;
        }

        public void fillInfoList()
        {
            FunctionaryInfo info = new FunctionaryInfo();
            foreach (Functionary e in allEmployees)
            {
                info = fillInfo(e);
                employeesToShow.Add(info);
            }
            listViewEmployees.DataContext = employeesToShow;
        }

        public FunctionaryInfo fillInfo(Functionary e)
        {
            FunctionaryInfo f = new FunctionaryInfo();
            f.CI = e.CI;
            f.Name = e.Name;
            f.Name = f.Name + " " + e.PaternalName;
            f.Rank = e.Rank;
            f.WorkingRole = e.WorkingRole;
            if (String.IsNullOrEmpty(e.MaternalName))
                f.Name = f.Name + " ";
            else
                f.Name = f.Name + " " + e.MaternalName;
            return f;
        }

        private void FilterResults()
        {
            FunctionaryInfo info = new FunctionaryInfo();
            employeesToShow.Clear();
            listViewEmployees.DataContext = null;
            if (!String.IsNullOrEmpty(tbCI.Text) && rbtnByCi.IsChecked == true)
            {
                foreach (Functionary e in allEmployees)
                {
                    if (containsString(e.CI.ToString(), tbCI.Text))
                    {
                        info = fillInfo(e);
                        employeesToShow.Add(info);
                    }
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(tbName.Text) && rbtnByName.IsChecked == true)
                {
                    foreach (Functionary e in allEmployees)
                    {
                        if (containsString(e.Name, tbName.Text) || containsString(e.PaternalName, tbName.Text) || containsString(e.MaternalName, tbName.Text))
                        {
                            info = fillInfo(e);
                            employeesToShow.Add(info);
                        }
                    }
                }
                else
                    fillInfoList();

            }
            listViewEmployees.DataContext = employeesToShow;
        }

        public bool containsString(string cad1, string cad2)
        {
            string aux = "";
            if (cad1 != null)
            {
                if (cad1.Length >= cad2.Length)
                {
                    for (int i = 0; i < cad2.Length; i++)
                    {
                        aux += cad2[i];
                    }
                    if (String.Equals(cad2, aux, StringComparison.CurrentCultureIgnoreCase))
                        return true;
                }
            }
            return false;
        }

        private void tbName_TextChanged(object sender, TextChangedEventArgs e)
        {
            FilterResults();
        }

        private void tbCI_TextChanged(object sender, TextChangedEventArgs e)
        {
            FilterResults();
        }

        public Functionary findEmployee()
        {
            Functionary f = new Functionary();
            FunctionaryInfo info = new FunctionaryInfo();
            ListViewItem all = (ListViewItem)listViewEmployees.SelectedItem;
            /*info = all.Content;
            info = listViewEmployees.Items[listViewEmployees.SelectedIndex];*/


            return f;
        }
        
        private void rbtnByName_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbName.IsEnabled = true;
            tbCI.IsEnabled = false;
        }

        private void rbtnByCi_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tbName.IsEnabled = false;
            tbCI.IsEnabled = true;
        }

        private void listViewEmployees_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            Employee = (listViewEmployees.SelectedItem as FunctionaryInfo);
            getEmployee(sender, e);
            Window.GetWindow(this).Close();
        }

        private void listViewEmployees_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listViewEmployees.SelectedValue as FunctionaryInfo != null)
            {
                Employee = (listViewEmployees.SelectedValue as FunctionaryInfo);
                if(getEmployee!=null)
                    getEmployee(sender, e);
                Window.GetWindow(this).Close();
            }
        }
    }
}


